package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.getScreenWidth
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator


class SyPagingHeader2Adapter(val manager: RequestManager, val position: Int, val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    lateinit var goodsIndex: GoodsIndex

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {
        val binding = holder.binding as PagingitemSyheader2Binding

        var list: List<GoodsIndex.Banner>? = null
        when (position) {
            0 -> list = goodsIndex.banner_list_hy
            1 -> list = goodsIndex.banner_list_jf
            2 -> list = goodsIndex.banner_list_lp
            3 -> list = goodsIndex.banner_list_yx
        }

        list?.let {
            binding.banner.setAdapter(object : SyBannerAdapter<GoodsIndex.Banner>(it) {
                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: GoodsIndex.Banner?,
                    position: Int,
                    size: Int
                ) {
                    manager.load(data?.image_url)
                        .apply(RequestOptions()
                            .error(R.mipmap.ic_launcher)
                        ).into(
                            holder!!.imageView
                        )
                }
            })
        }
        binding.banner.start()

    }


    fun itemClick(bean: GoodsIndex.JxGoods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
        ) {
            lp.isFullSpan = true
        }
    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.pagingitem_syheader2,
                parent,
                false
            )
        ).apply {
            val itemBinding = this.binding as PagingitemSyheader2Binding
            itemBinding.event = this@SyPagingHeader2Adapter

            val width = getScreenWidth(parent.context)
            val params: ViewGroup.LayoutParams = itemBinding.banner.getLayoutParams()
            with(itemBinding) {
                params.height = (width / 2.8f).toInt()//2.25f
                banner.setLayoutParams(params)
                banner.setIndicator(CircleIndicator(context))
            }

        }
    }


}